import {_decorator, Component, Node, Label, Prefab, instantiate,director} from 'cc';
import {NetManager} from "../manager/NetManager";

const {ccclass, property} = _decorator;

@ccclass('LoginButton')
export class LoginButton extends Component {

    @property({type: Node})
    private userNameInput: Node;

    @property({type: Prefab})
    private tip: Prefab;

    clickLogin(): void {
        const userName: string = (this.userNameInput.getComponent(Label).string);
        if (NetManager.getInstance().isConnected() && userName) {
            NetManager.getInstance().sendApiMsg(userName, (res: any) => {
                if (res.isSucc) {
                    director.loadScene('BattleScene');
                    console.log(res.res.userId);
                } else {
                    this.tip = instantiate(this.tip);
                    this.tip.setParent(this.node);
                    this.tip.getComponent(Label).string = res.err.message;
                    setTimeout(function () {
                        this.tip.getComponent(Label).string = null;
                    }.bind(this), 1500);
                }
            })
        }
    }
}


